package cn.upfinder.allvideo.Presenter;

import android.content.Context;
import android.util.Log;

import java.util.ArrayList;

import cn.upfinder.allvideo.Contract.VideoListContract;
import cn.upfinder.datalibrary.Api.ApiManage;
import cn.upfinder.datalibrary.Bean.VideoEntity;
import rx.Observer;

/**
 * Created by upfinder on 2016/11/27.
 */

public class VideoListPresenter implements VideoListContract.Presenter {
    private static final String TAG = VideoListPresenter.class.getSimpleName();

    private Context context;
    private VideoListContract.View mvpView;


    public VideoListPresenter(Context context, VideoListContract.View mvpView) {

        this.context = context;
        this.mvpView = mvpView;
    }


    @Override
    public void getMovies(int page) {

        ApiManage.getInstence().getYoukuMovie(context, page, new Observer<ArrayList<VideoEntity>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Log.e(TAG, "onError: ", e);
                mvpView.showError(e.getMessage());
            }

            @Override
            public void onNext(ArrayList<VideoEntity> videoEntities) {

                Log.d(TAG, "onNext: 获取优酷电影共 " + videoEntities.size());
                mvpView.showMovie(videoEntities);
            }
        });
    }

    @Override
    public void getMoreMovies(int page) {

        ApiManage.getInstence().getYoukuMovie(context, page, new Observer<ArrayList<VideoEntity>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                mvpView.showError(e.getMessage());
            }

            @Override
            public void onNext(ArrayList<VideoEntity> videoEntities) {

                mvpView.showMoreMovie(videoEntities);
            }
        });
    }


    @Override
    public void getYouKuPlay(int page) {
        ApiManage.getInstence().getYouKuPlay(context, page, new Observer<ArrayList<VideoEntity>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Log.e(TAG, "onError: ", e);
                mvpView.showError(e.getMessage());
            }

            @Override
            public void onNext(ArrayList<VideoEntity> videoEntities) {

                mvpView.showMovie(videoEntities);
            }
        });
    }

    @Override
    public void getYoukuMorePlay(int page) {

        ApiManage.getInstence().getYouKuPlay(context, page, new Observer<ArrayList<VideoEntity>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Log.e(TAG, "onError: ", e);
                mvpView.showError(e.getMessage());
            }

            @Override
            public void onNext(ArrayList<VideoEntity> videoEntities) {

                mvpView.showMoreMovie(videoEntities);
            }
        });
    }

    @Override
    public void getSouKuBeautyDance(int page) {
        ApiManage.getInstence().getSouKuBeautyDanceList(context, page, new Observer<ArrayList<VideoEntity>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Log.e(TAG, "onError: ", e);
                mvpView.showError(e.getMessage());
            }

            @Override
            public void onNext(ArrayList<VideoEntity> videoEntities) {
                Log.d(TAG, "onNext: 获取美女热舞数据》》" + videoEntities.size());
                if (videoEntities.size() % 2 != 0) {
                    videoEntities.remove(videoEntities.size() - 1);
                }
                mvpView.showMovie(videoEntities);
            }
        });
    }

    @Override
    public void getMoreSouKuBeautyDance(int page) {

        ApiManage.getInstence().getSouKuBeautyDanceList(context, page, new Observer<ArrayList<VideoEntity>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Log.e(TAG, "onError: ", e);
                mvpView.showError(e.getMessage());
            }

            @Override
            public void onNext(ArrayList<VideoEntity> videoEntities) {

                Log.d(TAG, "onNext: 加载美女热舞数据》》" + videoEntities.size());
                mvpView.showMoreMovie(videoEntities);
            }
        });
    }

    @Override
    public void start() {

    }
}
